#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

/*Private procedures and functions.*/
static bool IsMeasurable(int target, int sampleWeights[], int nSampleWeights);
static void inputWeights(int sampleWeights[], int arraySize, int *maxIndex, int *sumWeights);
static void printGroupMembers();

/*Main function.*/
main(){

	int i, sumWeights, maxIndex, sampleWeights[10];

	i = sumWeights = maxIndex = 0;

	printGroupMembers();

	inputWeights(sampleWeights, 10, &maxIndex, &sumWeights);
	
	for (i = 1; i <= sumWeights; i++){			/*Print all measurable weights.*/
		if (IsMeasurable(i, sampleWeights, maxIndex)){
			printf("%d is measurable\n", i);
		}
		else{
			printf("%d is NOT measurable\n", i);
		}
	}
}

/*
* Procedure: printGroupMembers
* Usage: printGroupMembers();
* --------------------------------
* This procedure prints the authors of this
* program to screen.
*/
static void printGroupMembers(){

	printf("This program was developed by: ");
	printf("Jim Glansk, S141587, ");
	printf("Henrik Wistbacka, S142066\n");
}

/*
* Procedure: IsMeasurable
* Usage: result = IsMeasurable(target, sampleWeights, maxIndex);
* --------------------------------
* This function test if a weight target is 
* measurable. Input is weight target, an array
* with available weights and an index maxIndex,
* which is the last index used in the array
* sampleWeights. Output is TRUE if target is
* possible to measure with the sampleWeights.
* Output is FALSE if target cannot be measured with
* available sampleWeights.
*/
static bool IsMeasurable(int target, int sampleWeights[], int maxIndex){

	if (target == 0){
		return(TRUE);
	}
	else if (maxIndex < 0){
		return(FALSE);
	}
	else{
		return(IsMeasurable((target - sampleWeights[maxIndex]), sampleWeights, maxIndex - 1) ||
			IsMeasurable(target, sampleWeights, maxIndex - 1) ||
			IsMeasurable((target + sampleWeights[maxIndex]), sampleWeights, maxIndex - 1));
	}
}

/*
* Procedure: inputWeights
* Usage: inputWeights(sampleWeights, arraySize,  &maxindex, &sumWeights);
* --------------------------------
* This procedure asks the user which weights which will be used
* in the measure. Input array sampleWeights will contain the user input.
* At most a number of arraySize weights
* will be used and is the size of the input array sampleWeights. 
* Output is maxIndex, which is the last index used in the
* array sampleWeights. Output sumWeights is the maximum weight that
* can be measured with available weights in sampleWeights. The user
* finishes the input of weights with a -1.
*/
static void inputWeights(int sampleWeights[], int arraySize, int *maxIndex, int *sumWeights){

	int i, weight, nSampleWeights;

	i = *sumWeights = nSampleWeights = 0;

	printf("What's Measurable\n");
	printf("Input all available weights, end with (-1):\n");


	while (nSampleWeights < arraySize){
		weight = GetInteger();
		if (weight == -1){
			break;
		}
		sampleWeights[nSampleWeights++] = weight;
		*sumWeights += weight;
	}

	*maxIndex = nSampleWeights--;
}